using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

public class StarCamera : MonoBehaviour
{
	public GUISkin gSkin;
	public GUISkin altSkin;
	public Planet planet;
	public Kingdom kd;						// PlayerKingdom is not used to reserve the right to camera on enemy planets


	private Rect rectWin = new Rect(10, 10, 300, 540);
	private Rect rectSelected = new Rect(Screen.width / 2 - 100, Screen.height - 210, 200, 120);
	private Rect rectDispatch = new Rect(Screen.width - 220, 10, 160, 120);
	private Rect rectWarship = new Rect(Screen.width - 220, 180, 160, 120);
	
	private Vector2 winscrollPos = Vector2.zero;
	private Vector2 winscrollPos1 = Vector2.zero;
	private Vector2 winscrollPos2 = Vector2.zero;
	private Vector2 winscrollPos3 = Vector2.zero;


	// Use this for initialization
	void Start ()
	{

	}
	
	// Update is called once per frame
	void LateUpdate ()
	{
		transform.position = new Vector3(1000.0f, 0, 1000.0f);
		transform.rotation = new Quaternion(90.0f, 0.0f, 0.0f, 0.0f);
	}

	void OnGUI()
	{
		GUI.skin = gSkin;

		GUI.Label(new Rect(Screen.width / 2 - 60, 0, 120, 50), planet.PlanetName + " : (" + planet.Kingdom.Race.RaceType.ToString() + ")", gSkin.customStyles[0]);

		if (GUI.Button(new Rect(Screen.width - 100, Screen.height - 25, 100, 25), "Return", gSkin.button))
		{
			Core.EnableMainCamera();
		}

		rectWin = GUILayout.Window(10, rectWin, WinFacilityList, "Select Development");

		if (planet.DevObj.Count > 0)
			rectSelected = GUILayout.Window(11, rectSelected, WinQueueList, "Next Development");

		if (planet.Dispatch.Count > 0)
			rectDispatch = GUILayout.Window(12, rectDispatch, WinDispatchList, "Dispatchables");

		if (planet.Warship.Count > 0)
			rectWarship = GUILayout.Window(13, rectWarship, WinWarshipList, "Warships");
	}

	// windows
	void WinWarshipList(int windowID)
	{
		winscrollPos3 = GUILayout.BeginScrollView(winscrollPos3, GUILayout.Width(160), GUILayout.Height(145));

		for (int i = 0; i < planet.Warship.Count; i++)
		{
			if (GUILayout.Button(planet.Warship[i].DisplayName, gSkin.textField))
			{
				bool toadd = false;

				if (planet.DevObj.Count < Core.MaxDevQueue)
					toadd = true;

				if (toadd && planet.Warship[i] is IRepeatable && ((IRepeatable)planet.Warship[i]).CheckRepeat(planet))
					toadd = true;

				if (toadd)
					planet.DevObj.Add(planet.Warship[i] as IBuildable);
			}
		}

		GUILayout.EndScrollView();
	}

	void WinDispatchList(int windowID)
	{
		winscrollPos2 = GUILayout.BeginScrollView(winscrollPos2, GUILayout.Width(160), GUILayout.Height(125));

		for (int i = 0; i < planet.Dispatch.Count; i++)
		{
			if (GUILayout.Button(((IBuildable)planet.Dispatch[i]).DisplayName, gSkin.textField))
			{
				bool toadd = false;

				if (planet.DevObj.Count < Core.MaxDevQueue)
					toadd = true;

				if (toadd && planet.Dispatch[i] is IRepeatable && ((IRepeatable)planet.Dispatch[i]).CheckRepeat(planet))
					toadd = true;

				if (toadd && !(planet.Dispatch[i] is IRepeatable))
					foreach (IBuildable bd in planet.DevObj)
						if (bd.DisplayName == ((IBuildable)planet.Dispatch[i]).DisplayName)
							toadd = false;

				if (toadd)
					planet.DevObj.Add(planet.Dispatch[i] as IBuildable);
			}
		}

		GUILayout.EndScrollView();
	}

	void WinQueueList(int windowID)
	{
		winscrollPos1 = GUILayout.BeginScrollView(winscrollPos1, GUILayout.Width(200), GUILayout.Height(145));

		for (int i = 0; i < planet.DevObj.Count; i++)
		{
			IBuildable ib = planet.DevObj[i] as IBuildable;

			if (GUILayout.Button(ib.DisplayName, altSkin.button))
			{
				if (planet.DevObj[i] is Planet.PlanetFacility && ((Planet.PlanetFacility)planet.DevObj[i]).pBuild is ColonyBase)
				{
					planet.Solar.ColonyBase[2] -= 1;
					planet.Solar.ColonyBase[3] -= 1;
				}

				planet.DevObj.RemoveAt(i--);
			}
		}

		GUILayout.EndScrollView();
	}

	void WinFacilityList(int windowID)
	{
		winscrollPos = GUILayout.BeginScrollView(winscrollPos, GUILayout.Width(300), GUILayout.Height(540));
		GUILayout.BeginVertical(gSkin.box);

		for (int i = 0; i < planet.Facilities.Count; i++)
		{
			if (planet.Facilities[i].pBuild is IRepeatable)
				planet.Facilities[i].pRepeat = ((IRepeatable)planet.Facilities[i].pBuild).CheckRepeat(planet);

			if (planet.Facilities[i].pRepeat || (!(planet.Facilities[i].pBuild is IRepeatable) && !planet.Facilities[i].pInEffect && !planet.Facilities[i].pObsolete))
			{
				if (GUILayout.Button(planet.Facilities[i].DisplayName, gSkin.textField))
				{
					bool toadd = false;

					if (planet.DevObj.Count < Core.MaxDevQueue)
						toadd = true;

					if (planet.Facilities[i].pBuild is ColonyBase && ((ColonyBase)planet.Facilities[i].pBuild).CheckRepeat(planet))
					{
						planet.Solar.ColonyBase[2] += 1;
						planet.Solar.ColonyBase[3] += 1;
					}

					if (toadd && planet.Facilities[i].pBuild is IRepeatable && ((IRepeatable)planet.Facilities[i].pBuild).CheckRepeat(planet))
						toadd = true;

					if (toadd)
						foreach (IBuildable bda in planet.DevObj)
							if (bda is Planet.PlanetFacility && ((Planet.PlanetFacility)bda).pBuild.Name == planet.Facilities[i].pBuild.Name && !planet.Facilities[i].pRepeat)
								toadd = false;

					if (toadd)
						planet.DevObj.Add(planet.Facilities[i] as IBuildable);
				}
			}
		}


		GUILayout.EndVertical();
		GUILayout.EndScrollView();

		// GUI.DragWindow();
	}
}
